package com.javaxiaobear.module.monitor.mapper;

import static com.javaxiaobear.module.monitor.domain.table.SysJobTableDef.SYS_JOB;

import com.javaxiaobear.base.common.utils.PageUtils;
import com.javaxiaobear.module.monitor.domain.SysJob;
import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import java.util.Arrays;
import java.util.List;

/**
 * 调度任务信息 数据层
 *
 * @author javaxiaobear
 */
public interface SysJobMapper extends BaseMapper<SysJob> {
  /**
   * 查询调度任务日志集合
   *
   * @param job 调度信息
   * @return 操作日志集合
   */
  default List<SysJob> selectJobList(SysJob job) {
    QueryWrapper queryWrapper = QueryWrapper.create();

    if (job.getJobName() != null && !job.getJobName().isEmpty()) {
      queryWrapper.and(SYS_JOB.JOB_NAME.like(job.getJobName()));
    }

    if (job.getJobGroup() != null && !job.getJobGroup().isEmpty()) {
      queryWrapper.and(SYS_JOB.JOB_GROUP.eq(job.getJobGroup()));
    }

    if (job.getStatus() != null) {
      queryWrapper.and(SYS_JOB.STATUS.eq(job.getStatus()));
    }

    if (job.getInvokeTarget() != null && !job.getInvokeTarget().isEmpty()) {
      queryWrapper.and(SYS_JOB.INVOKE_TARGET.like(job.getInvokeTarget()));
    }

    // 排序
    if (job.getOrderByColumn() != null && !job.getOrderByColumn().isEmpty()) {
      String orderBy =
          job.getOrderByColumn() + " " + (job.getIsAsc() != null ? job.getIsAsc() : "asc");
      queryWrapper.orderBy(orderBy);
    }

    return selectListByQuery(queryWrapper);
  }

  default Page<SysJob> selectJobPage(SysJob job) {
    QueryWrapper queryWrapper = QueryWrapper.create();

    if (job.getJobName() != null && !job.getJobName().isEmpty()) {
      queryWrapper.and(SYS_JOB.JOB_NAME.like(job.getJobName()));
    }

    if (job.getJobGroup() != null && !job.getJobGroup().isEmpty()) {
      queryWrapper.and(SYS_JOB.JOB_GROUP.eq(job.getJobGroup()));
    }

    if (job.getStatus() != null) {
      queryWrapper.and(SYS_JOB.STATUS.eq(job.getStatus()));
    }

    if (job.getInvokeTarget() != null && !job.getInvokeTarget().isEmpty()) {
      queryWrapper.and(SYS_JOB.INVOKE_TARGET.like(job.getInvokeTarget()));
    }

    // 排序
    if (job.getOrderByColumn() != null && !job.getOrderByColumn().isEmpty()) {
      String orderBy =
          job.getOrderByColumn() + " " + (job.getIsAsc() != null ? job.getIsAsc() : "asc");
      queryWrapper.orderBy(orderBy);
    }

    return paginate(PageUtils.getPage(queryWrapper), queryWrapper);
  }

  /**
   * 查询所有调度任务
   *
   * @return 调度任务列表
   */
  default List<SysJob> selectJobAll() {
    return selectAll();
  }

  /**
   * 通过调度ID查询调度任务信息
   *
   * @param jobId 调度ID
   * @return 角色对象信息
   */
  default SysJob selectJobById(Long jobId) {
    return selectOneById(jobId);
  }

  /**
   * 通过调度ID删除调度任务信息
   *
   * @param jobId 调度ID
   * @return 结果
   */
  default int deleteJobById(Long jobId) {
    return deleteById(jobId);
  }

  /**
   * 批量删除调度任务信息
   *
   * @param ids 需要删除的数据ID
   * @return 结果
   */
  default int deleteJobByIds(Long[] ids) {
    if (ids == null || ids.length == 0) {
      return 0;
    }
    return deleteBatchByIds(Arrays.asList(ids));
  }

  /**
   * 修改调度任务信息
   *
   * @param job 调度任务信息
   * @return 结果
   */
  default int updateJob(SysJob job) {
    return update(job);
  }

  /**
   * 新增调度任务信息
   *
   * @param job 调度任务信息
   * @return 结果
   */
  default int insertJob(SysJob job) {
    return insert(job);
  }
}
